package org.dtrd.modules.food.controller;


import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.food.entity.bean.CookbookCategoryDetail;
import org.dtrd.modules.food.entity.bean.CookbookCategoryListInfo;
import org.dtrd.modules.food.entity.request.CookbookCategoryListRequest;
import org.dtrd.modules.food.service.IDtrdExtEmsYwCookbookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 菜谱分类 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-09
 */
@Api(tags = "食物库")
@RestController
@RequestMapping("/100/16/")
public class CookbookCategoryController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwCookbookCategoryService cookbookCategoryService;

    @ApiOperation(value = "查询菜谱分类详情", response = CookbookCategoryDetail.class)
    @GetMapping("1001620")
    @AutoLog(value="查询菜谱分类详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCookbookCategory(@RequestParam Integer id){
        CookbookCategoryDetail info = cookbookCategoryService.getCookbookCategory(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询菜谱分类列表", response = CookbookCategoryListInfo.class)
    @PostMapping("1001619")
    @AutoLog(value="查询菜谱分类列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCookbookCategoryList(@RequestBody CookbookCategoryListRequest request){
        List<CookbookCategoryListInfo> list = cookbookCategoryService.getCookbookCategoryList(request);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改菜谱分类")
    @PostMapping("1001621")
    @AutoLog(value="新增或修改菜谱分类", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateCookbookCategory(@RequestBody CookbookCategoryDetail dto){
        if (StrUtil.isBlank(dto.getCategoryName())) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = cookbookCategoryService.saveOrUpdateCookbookCategory(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除菜谱分类")
    @DeleteMapping("1001622")
    @AutoLog(value="删除菜谱分类", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeCookbookCategory(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = cookbookCategoryService.removeCookbookCategory(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
